<?php

namespace App\Models\Stock;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;

class StockSub extends Model
{
    protected $table = 'stock_sub';
    public $timestamps = false;
    protected $primaryKey = 'id';
    protected $guarded = ['_token'];

    public static function getList(Request $request, $cpid)
    {
        $query = self::where('cpid', $cpid);

        $archive_no = $request->archive_no;
        if ($archive_no != '') {
            $query->where('archive_no', 'like', '%' . $archive_no . '%');
        }
        $archive_name = $request->archive_name;
        if ($archive_name != '') {
            $query->where('archive_name', 'like', '%' . $archive_name . '%');
        }
        $archive_spec = $request->archive_spec;
        if ($archive_spec != '') {
            $query->where('archive_spec', 'like', '%' . $archive_spec . '%');
        }
        $archive_material = $request->archive_material;
        if ($archive_material != '') {
            $query->where('archive_material', 'like', '%' . $archive_material . '%');
        }
        $warehouse_id = intval($request->warehouse_id);
        if ($warehouse_id > 0) {
            $query->where('warehouse_id', $warehouse_id);
        }

        $is_have_count = $request->is_have_count;
        if ($is_have_count == 1) { //库存大于0
            $query->where('can_use_pieces', '>', 0);
        }
        if ($is_have_count == 2) { //库存小于登录0
            $query->where('can_use_pieces', '<=', 0);
        }

        $stock_id = intval($request->stock_id);
        if ($stock_id > 0) {
            $query->where('stock_id', $stock_id);
        }

        $limit = $request->limit ?? 10;
        $list = $query->paginate($limit)->toArray();

        return paginateDataToArray($list);
    }

    public function getCanUseWeightAttribute($value)
    {
        return $value / 10;
    }

    public function getLockedWeightAttribute($value)
    {
        return $value / 10;
    }

    public function getCurrentWeightAttribute($value)
    {
        return $value / 10;
    }

    public function getStorageWaitingWeightAttribute($value)
    {
        return $value / 10;
    }

    public function getWeightAttribute($value)
    {
        return $value / 10;
    }
    public function getTotalWeightAttribute($value)
    {
        return $value / 10;
    }
}
